package cn.zxl.graduate.service.impl;

import cn.zxl.graduate.myenum.Constant;
import cn.zxl.graduate.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @Author: bo-feifei
 * @Date 2021/11/1 16:38
 * @Version 1.0
 */
@Service
public class RedisServiceImpl implements IRedisService {

    @Autowired
    private StringRedisTemplate template;

    @Override
    public void setKeyInRedis(String tokens,String str) {
        if (StringUtils.hasText(tokens)) {
            String[] split = tokens.split(",");
            String keyTime = split[0];
            String token = split[1];
            Long time1 = Long.parseLong(keyTime);
            double sureTime = (float)time1 / 60000F;
            if (sureTime > 1) {
                Long time = time1 / 60000;
                template.opsForValue().set(token, str, time, TimeUnit.MINUTES);
                return;
            }
            Long time = time1 / 1000;
            template.opsForValue().set(token,str,time,TimeUnit.SECONDS);
        }
    }

    @Override
    public Integer setWatchNumAdd(Long id) {
        Long increment = template.opsForValue().increment(Constant.ARTICLE_WATCH_NUM + ":" + id);
        return increment.intValue();
    }

    @Override
    public Integer setLikeNumAdd(Long id) {
        Long increment = template.opsForValue().increment(Constant.ARTICLE_LIKE_NUM + ":" + id);
        return increment.intValue();
    }
}
